package com.xinchuang.controller.targetSystem.system;

import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultEnum;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.object.*;
import com.xinchuang.fiter.NoToken;
import com.xinchuang.service.targetSystem.SystemBuildService;
import com.xinchuang.service.targetSystem.external.ApiService;
import com.xinchuang.util.FileElseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName TargetSystemController
 * @Description: MB系统接口
 * @auther xukl
 * @data 2021/3/25  10:56
 * @Version V1.0
 */
@Api(value = "MB系统controller", tags = {"目标系统维护接口targetSystem"})
@RestController
@RequestMapping("/targetSystem")
@Slf4j
public class SystemController {

    @Resource
    private SystemBuildService systemBuildService;

    @Resource
    private ApiService apiService;

    /**
     * 查询系统列表
     */
    private static final String SELECT_SYSTEM_LIST = "/queryList";
    /**
     * 查询系统基本信息
     */
    private static final String SELECT_SYSTEM_BASE_DATA = "/baseData";
    /**
     * 查询待选目标
     */
    private static final String SELECT_SYSTEM_WAIT_CHOICE_TARGET = "/findWait";
    /**
     * 更新系统目标选择
     */
    private static final String UPDATE_SYSTEM_TARGET_CHOICE = "/updateTargetList";
    /**
     * 查询已选目标
     */
    private static final String SELECT_SYSTEM_TARGET_CONFIRM = "/findConfirm";
    /**
     * 指标下拉列表
     */
    private static final String SELECT_INDEX_PULL = "/indexPull";
    /**
     * 查询系统待选信息列表
     */
    private static final String SELECT_WAIT_DATA_LIST = "/queryWaitList";
    /**
     * 添加待选目标到系统
     */
    private static final String INSERT_INDEX_TARGET_LIST = "/addTargetList";
    /**
     * 添加对比指数参数
     */
    private static final String SELECT_INDEX_COMPARISION_DATA = "/comparisionData";
    /**
     * 查询指数列表
     */
    private static final String SELECT_INDEX_DATA = "/indexData";
    /**
     * 查询系统已选信息列表
     */
    private static final String SELECT_CONFIRM_DATA_LIST = "/queryConfirmList";

    /**
     * 操作系统专有属性
     */
    private static final String SYSTEM_ELSE_DATA = "/elseData";

    /**
     * 生成报告
     */
    private static final String CREATE_DOC = "/createDoc";

    /**
     * 下载报告
     */
    private static final String DOWNLOAD_DOC = "/downDoc";

    @ApiOperation(value = "查询系统列表")
    @PostMapping(SELECT_SYSTEM_LIST)
    public Result getSystemList(
            @Validated @RequestBody SystemList systemList
    ) {
        return systemBuildService.selectSystemList(
                systemList.getPageNum(),
                systemList.getPageSize(),
                systemList.getClassify(),
                systemList.getKeyword());
    }

    @NoToken
    @ApiOperation(value = "查询系统基本信息")
    @GetMapping(SELECT_SYSTEM_BASE_DATA)
    public Result getSystemBaseData(
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "systemName", required = false) String systemName,
            @RequestParam(value = "targetType", required = false) String targetType,
            @RequestParam(value = "country", required = false) String country,
            @RequestParam(value = "remark", required = false) String remark,
            @RequestParam(value = "sysType", required = false) String sysType,
            @RequestParam(value = "url", required = false) String url
    ) {
        return systemBuildService.selectSystemOnly(sId, systemName, targetType, country, remark, sysType, url);
    }

    @NoToken
    @ApiOperation(value = "查询待选目标")
    @GetMapping(SELECT_SYSTEM_WAIT_CHOICE_TARGET)
    public Result getWaitChoiceTargetList(
            @RequestParam(value = "country", required = false) String country,
            @RequestParam(value = "targetType") String targetType,
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "type", required = false, defaultValue = "0") int status
    ) {
        return systemBuildService.selectSystemWaitTarget(country,targetType, sId, keyword, status);
    }

    @NoToken
    @ApiOperation(value = "更新系统目标选择")
    @PutMapping(UPDATE_SYSTEM_TARGET_CHOICE)
    public Result getUpdateTargetChoice(
            @RequestBody JSONObject jsonObject
    ) {
        String sId = jsonObject.getString("sId");
        String status = jsonObject.getString("status");
        if (null == jsonObject.getJSONArray("targetList") || jsonObject.getJSONArray("targetList").isEmpty()) {
            return ResultUtil.error(ResultEnum.ADD_TARGET_DATA_LIST_ERROR);
        }
        List<String> targetList = jsonObject.getJSONArray("targetList").toJavaList(String.class);
        return systemBuildService.addChoiceTarget(sId, targetList, status);
    }

    @ApiOperation(value = "查询已选目标")
//    @GetMapping(SELECT_SYSTEM_TARGET_CONFIRM)
    public Result getConfirmChoiceTargetList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "keyword", required = false) String keyword
    ) {
        return systemBuildService.selectSystemConfirmTarget(pageNum, pageSize, sId, keyword);
    }

    @ApiOperation(value = "新增/更新系统基本信息")
    @PostMapping(SELECT_SYSTEM_BASE_DATA)
    public Result getAddSystem(
            @Validated @RequestBody SystemDataAdd systemDataAdd,
            HttpServletResponse response
    ) {
        synchronized (this) {
            if (systemBuildService.judgeSystemName(systemDataAdd.getSId(),systemDataAdd.getSystemName())) {
                return ResultUtil.error(ResultEnum.SYSTEMS_NAME_EXIST);
            }
            return systemBuildService.addSystem(systemDataAdd, StringUtils.isNotBlank(response.getHeader("userId")) ? response.getHeader("userId") : "0" + "&&" + systemDataAdd.getSysType());
        }
    }

    @NoToken
    @ApiOperation(value = "删除系统")
    @DeleteMapping(SELECT_SYSTEM_BASE_DATA)
    public Result getAddSystem(
            @RequestParam(value = "sIds") String sIds
    ) {
        List<String> targetId = FileElseUtil.getStringTransitionList(sIds);
        if (null == targetId || targetId.isEmpty()) {
            return ResultUtil.error(30005, ResultEnum.getByValue(30005));
        }
        return apiService.deleteSystem(targetId);
    }

    @NoToken
    @ApiOperation(value = "查询单个系统指标列表")
    @GetMapping(SELECT_INDEX_PULL)
    public Result getSystemIndexList(
            @RequestParam(value = "sId") String sId
    ) {
        return apiService.selectIndexPull(sId);
    }

    @NoToken
    @ApiOperation(value = "查询系统待选信息列表")
    @GetMapping(SELECT_WAIT_DATA_LIST)
    public Result getSystemWaitDataList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "index", required = false) String index
    ) {
        return apiService.selectSystemWaitList(pageNum, pageSize, sId, index);
    }

    @NoToken
    @ApiOperation(value = "添加待选目标到系统")
    @PostMapping(INSERT_INDEX_TARGET_LIST)
    public Result addWaitDataToConfirmData(
            @Validated @RequestBody SystemIndexAdd systemIndexAdd
    ) {
        return apiService.insertConfirmIndex(systemIndexAdd.getRelationList(), systemIndexAdd.getSId(), systemIndexAdd.getStatus());
    }

    @NoToken
    @ApiOperation(value = "查询指数列表")
    @GetMapping(SELECT_INDEX_DATA)
    public Result selectIndexData(
            @RequestParam(value = "sId") String sId
    ) {
        return apiService.selectSystemIndexList(sId);
    }

    @NoToken
    @ApiOperation(value = "查询对比指数列表")
    @GetMapping(SELECT_INDEX_COMPARISION_DATA)
    public Result selectIndexComparisionData(
            @RequestParam(value = "sId") String sId
    ) {
        return apiService.selectComparisonIndex(sId);
    }

    @NoToken
    @ApiOperation(value = "查询系统已选信息列表")
    @GetMapping(SELECT_CONFIRM_DATA_LIST)
    public Result getSystemConfirmDataList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "index", required = false) String index,
            @RequestParam(value = "taskId", required = false) Integer taskId
    ) {
        return apiService.selectSystemConfirmList(pageNum, pageSize, sId, index, taskId);
    }

    @NoToken
    @ApiOperation(value = "查询系统专有属性")
    @GetMapping(SYSTEM_ELSE_DATA)
    public Result selectSystemElseData(
            @RequestParam(value = "sId") String sId
    ) {
        return systemBuildService.selectSystemElseData(sId);
    }

    @NoToken
    @ApiOperation(value = "新增/更新系统专有属性")
    @PostMapping(SYSTEM_ELSE_DATA)
    public Result addSystemElseData(
            @Validated @RequestBody SystemElseAdd elseAdd
            ) {
        return systemBuildService.addSystemElseData(elseAdd.getSid(), elseAdd.getSystemList());
    }

    @NoToken
    @ApiOperation(value = "生成报告")
    @GetMapping(CREATE_DOC)
    public Result createDoc(
            @RequestParam(value = "sId") String sId,
            @RequestParam(value = "damage") String damage
    ) {
        return systemBuildService.createDoc(sId, damage);
    }

    @NoToken
    @ApiOperation(value = "下载报告")
    @GetMapping(DOWNLOAD_DOC)
    public Result downDoc(
            @RequestParam(value = "sId") String sId
    ) {
        return systemBuildService.downDoc(sId);
    }

}
